<?php
$DEVELOPMENT=1;
$CMD_SCRIPT = 'heyu';
$LOG_FILE = '/tmp/heyu.log';
$X10_CONF_FILE = '/etc/heyu/x10.conf';

// DEVELOPMENT
if ($DEVELOPMENT == 1) {
	$X10_CONF_FILE = '../../conf/x10.conf';
	$LOG_FILE = 'heyu.log';
}

if ($_REQUEST ['action'] == 'list') {
	readX10Config();
} else {
	$txt = $_REQUEST ['entries'];
	executeCommand($txt);
	echo '{"return":"1"}';
}

function executeCommand($txt) {
	global $CMD_SCRIPT;
	$json = json_decode($txt);
	$cmdLine = $CMD_SCRIPT.' ';
	if ($json->category == 'Makros') {
		$cmdLine .= $json->name;
	}
	else {
		$cmdLine .= $json->status.' '.$json->id;
	}
	$cmdReturn = exec($cmdLine);
	logEntry('['.date("Y.m.d H:i:s").'] '.$cmdLine.' ['.$cmdReturn.']');
}

function logEntry($txt) {
	global $LOG_FILE;
	$myfile = fopen ( $LOG_FILE, "w" ) or die ( "Unable to open log file!" );
	fwrite ( $myfile, $txt );
	fclose ( $myfile );
}

function readX10Config() {
	global $X10_CONF_FILE;
	$myfile = fopen ( $X10_CONF_FILE, "r" ) or die ( "Unable to open config file!" );
	$componentString = '[';
	$macroString = '[';
	$separator = '';
	$deviceStatusList = getDeviceStatus();
	while(!feof($myfile)) {
		$line = fgets($myfile);
		if (substr($line, 0, 5) == 'ALIAS') {
			$tokens = preg_split("/[\s,]+/", $line);
			$index = substr($tokens[2],1);
			$componentString .= $separator;
			$componentString .= buildComponentString($tokens[2], $tokens[1], $tokens[5], $deviceStatusList[$index], $tokens[3], '');
			$separator=',';
		}
		if (substr($line, 0, 5) == 'SCENE') {
			$tokens = preg_split("/[\s,]+/", $line);
			$tempInfecedControlls = getInfecedControlls($tokens);
			$componentString .= $separator;
			$componentString .= buildComponentString($tokens[2], $tokens[1], 'Makros', '', '', $tempInfecedControlls);
			$separator=',';
		}
	}
	$componentString .= ']';
	$macroString .= ']';
	echo $componentString;
}

function buildComponentString($anId, $aName, $aCategory, $aStatus, $aType, $someInfecedControlls) {
	if (strlen($someInfecedControlls) == 0) {
		$someInfecedControlls = '""';
	}
	$componentString = '{"category":"' .$aCategory. '",';
	$componentString .= '"name":"' . $aName . '",';
	$componentString .= '"type":"'. $aType .'",';
	$componentString .= '"infectedControlls":' . $someInfecedControlls . ',';
	$componentString .= '"status":"' .$aStatus. '",';
	$componentString .= '"id":"' . $anId . '"}';
	return $componentString;
}

function getInfecedControlls($tokens) {
	$tempMax = sizeof($tokens);
	$componentString = '[';
	$separator = '';
	$i = 2;
	while ($i < $tempMax-1) {
		$componentString .= $separator;
 		$tempName = $tokens[$i+1];
		if (strpos($tempName,';') > 1) {
			$tempName = substr($tempName,0,strlen($tempName)-1);
		}
		$componentString .= buildComponentString($tempName, $tempName, '', $tokens[$i], '', '');
		$i = $i +2;
		$separator=',';
	}
	$componentString .= ']';
	return $componentString;
}
function getDeviceStatus() {
	$line = readStatusOfMonitoredDevices();
	$statusArray = array();
	for ($i=16; $i>=0;$i--) {
		$status = substr($line,	$i,1);
		$statusArray[] = $status==0?'off':'on';
	}
	return $statusArray;
}

function readStatusOfMonitoredDevices() {
	global $DEVELOPMENT;
	if ($DEVELOPMENT == 1) {
 		return '0000100000010000';
	}
	$cmdReturn = array();
	exec('heyu info', $cmdReturn);
	return substr($cmdReturn[11], 38, 16);
}
?>